
//
// 120515 - AH - Created.
//

package org.soekrat.server.serialization;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *   What has been retrieved.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class RetrievedSerializable 
{
  private String name=null;
  
  private List<String> links=null;
  
  private String config=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public RetrievedSerializable(){}
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param name {@link #name()}.
   */
  public RetrievedSerializable(String name){ name(name); }
  
  /**
   * <p>
   *   Gets name.
   * </p>
   * @return Name or {@code null} if not sat.
   */
  public String name(){ return this.name; }
  
  /**
   * <p>
   *   Sets name.
   * </p>
   * @param name Name. 
   * @return {@code this}.
   */
  public RetrievedSerializable name(String name)
  {
    if (name==null)
    {
      throw new NullPointerException("name");
    }
    
    this.name=name; 
    return this; 
  }
  
  /**
   * <p>
   *   Adds 
   *   {@code String} representation of link associated with {@code this}.
   * </p>
   * @param lr Link's 
   *           {@code String} representation.
   * @return {@code this}.
   */
  public RetrievedSerializable link(String lr)
  {
    if (lr==null)
    {
      throw new NullPointerException("lr");
    }
    
    if (links==null) links=new ArrayList<String>();
    if (!links.contains(lr)) links.add(lr);
    return this;
  }
  
  /**
   * <p>
   *   Gets 
   *   {@code String} representation of links associated with {@code this}.
   * </p>
   * @param lr Link's 
   *           {@code String} representation.
   * @return {@code this}.
   */
  public String[] links()
  {
    return links==null ? E_STRING_A : links.toArray(E_STRING_A); 
  }
  
  /**
   * <p>
   *   Gets 
   *   {@link String} representation for link to configuration file associated
   *   with {@code this}.
   * </p>
   * @return Link.
   */
  public String config(){ return config; }
  
  /**
   * <p>
   *   Sets 
   *   {@link String} representation for link to configuration file associated
   *   with {@code this}.
   * </p>
   * @param lr
   * @return
   */
  public RetrievedSerializable config(String lr)
  {
    if (lr==null)
    {
      throw new NullPointerException("lr");
    }
    
    config=lr;
    return this;
  }
  
  @Override
  public String toString() 
  {
    StringBuilder sb=new StringBuilder();
    
    boolean first=true;
    if (name!=null){ sb.append("name=").append(name); first=false; }
    if (links!=null)
    {
      if (!first) sb.append(", ");
      sb.append("links=").append(links);
      first=false;
    }
    if (config!=null)
    {
      if (!first) sb.append(", ");
      sb.append("first=").append(first);
    }
    
    return sb.toString();
  }
  
  private static final String[] E_STRING_A=new String[0];
  
}
